package ve.com.ucv.changedetection.io;

import ve.com.ucv.changedetection.model.ConnectionManager;
import ve.com.ucv.changedetection.objects.Constants;
import ve.com.ucv.changedetection.objects.responses.CrawlerResponse;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

/**
 * Created by josmaguirre on 1/27/15.
 */
public class CrawlerIO extends ConnectionManager{
    Logger logger = Logger.getLogger(CrawlerIO.class);
    CrawlerResponse crawlerResponse = new CrawlerResponse();

    public CrawlerResponse getCrawlsWhitoutComparations(){
        Connection conn = getMySQLConnection();
        try{
            CallableStatement statement = conn.prepareCall(Constants.GET_CRAWLS_WITHOUT_COMPARATIONS);
            ResultSet result = statement.executeQuery();
            ArrayList<String> crawls = new ArrayList<String>();
            while (result.next()){
                crawls.add(result.getString("nombre"));
            }
            getCrawlerResponse().setCrawlsWithoutComparation(crawls);
        }catch(SQLException e){
            logger.error(e);
            crawlerResponse.setMessage(e.getMessage());
            crawlerResponse.setMessageCode("ERROR");
        }catch(Exception e){
            logger.error(e);
            crawlerResponse.setMessageCode("ERROR");
            crawlerResponse.setMessage(e.getMessage());
        }
        return crawlerResponse;
    }

    public CrawlerResponse getCrawlerResponse() {
        return crawlerResponse;
    }

    public void setCrawlerResponse(CrawlerResponse crawlerResponse) {
        this.crawlerResponse = crawlerResponse;
    }
}